{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ICESat-2 Data Download "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook orders and downloads ICESat-2 ATL03 data in the Beaufort, Chukchi, and Laptev Sea regions for the ICESat-2 hackweek project *Overcast*.\n",
    "\n",
    "It also has capability to gather data from any ICESat-2 data product in any region and timeframe of interest, based on user-defined input.\n",
    "\n",
    "The notebook makes use of *icepyx* functionality, and writes data to respective folders in shared-data on the hackweek's JupyterHub (at least for now); it borrows heavily from the tutorial material for the same hackweek, compiled by Jessica "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from icepyx import icesat2data as ipd\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set up order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Project name:  overcast\n",
      "name of region of interest:  beaufort\n",
      "ICESat-2 product short name:  ATL03\n"
     ]
    }
   ],
   "source": [
    "project_name = input(\"Project name: \")\n",
    "roi = input(\"name of region of interest: \")\n",
    "dataset= input(\"ICESat-2 product short name: \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "short_name = dataset\n",
    "\n",
    "if 'overcast' == project_name:\n",
    "    if 'beaufort' == roi:\n",
    "        spatial_extent = [-140,68,-120,80]\n",
    "    elif 'chukchi' == roi:\n",
    "        spatial_extent = [170,70,190,75]\n",
    "    elif 'laptev' == roi:\n",
    "        spatial_extent = [110,73,140,80]\n",
    "    else:\n",
    "        spatial_extent = [float(input(\"west: \")), float(input(\"low lat: \")) , float(input(\"east: \")), float(input(\"high lat: \"))]\n",
    "    \n",
    "    date_range = ['2019-06-10','2019-06-16']\n",
    "else:\n",
    "    spatial_extent = [float(input(\"west: \")), float(input(\"low lat: \")) , float(input(\"east: \")), float(input(\"high lat: \"))]\n",
    "    date_range = [input(\"start date: \"), input(\"end date: \")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ATL03_20190610002205_11110304_003_01.h5',\n",
       " 'ATL03_20190610002715_11110305_003_01.h5',\n",
       " 'ATL03_20190610015623_11120304_003_01.h5',\n",
       " 'ATL03_20190610020132_11120305_003_01.h5',\n",
       " 'ATL03_20190610111642_11180303_003_01.h5',\n",
       " 'ATL03_20190610112207_11180304_003_01.h5',\n",
       " 'ATL03_20190610125059_11190303_003_01.h5',\n",
       " 'ATL03_20190610125625_11190304_003_01.h5',\n",
       " 'ATL03_20190611013044_11270304_003_01.h5',\n",
       " 'ATL03_20190611013553_11270305_003_01.h5',\n",
       " 'ATL03_20190611122520_11340303_003_01.h5',\n",
       " 'ATL03_20190611123045_11340304_003_01.h5',\n",
       " 'ATL03_20190612010504_11420304_003_01.h5',\n",
       " 'ATL03_20190612011013_11420305_003_01.h5',\n",
       " 'ATL03_20190612115941_11490303_003_01.h5',\n",
       " 'ATL03_20190612120506_11490304_003_01.h5',\n",
       " 'ATL03_20190613003925_11570304_003_01.h5',\n",
       " 'ATL03_20190613004434_11570305_003_01.h5',\n",
       " 'ATL03_20190613113401_11640303_003_01.h5',\n",
       " 'ATL03_20190613113926_11640304_003_01.h5',\n",
       " 'ATL03_20190614001346_11720304_003_01.h5',\n",
       " 'ATL03_20190614001855_11720305_003_01.h5',\n",
       " 'ATL03_20190614014803_11730304_003_01.h5',\n",
       " 'ATL03_20190614015312_11730305_003_01.h5',\n",
       " 'ATL03_20190614110822_11790303_003_01.h5',\n",
       " 'ATL03_20190614111347_11790304_003_01.h5',\n",
       " 'ATL03_20190614124240_11800303_003_01.h5',\n",
       " 'ATL03_20190614124805_11800304_003_01.h5',\n",
       " 'ATL03_20190615012224_11880304_003_01.h5',\n",
       " 'ATL03_20190615012733_11880305_003_01.h5',\n",
       " 'ATL03_20190615121700_11950303_003_01.h5',\n",
       " 'ATL03_20190615122225_11950304_003_01.h5',\n",
       " 'ATL03_20190616005644_12030304_003_01.h5',\n",
       " 'ATL03_20190616010154_12030305_003_01.h5',\n",
       " 'ATL03_20190616115121_12100303_003_01.h5',\n",
       " 'ATL03_20190616115646_12100304_003_01.h5']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "region = ipd.Icesat2Data(short_name, spatial_extent, date_range) \n",
    "region.avail_granules(ids=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "earthdata_uid = 'molly.m.wieringa_2019'\n",
    "email = 'molly.m.wieringa@gmail.com'\n",
    "\n",
    "region.earthdata_login(earthdata_uid, email)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region.order_granules(subset=False, format = 'HDF5', email=False)\n",
    "print(\"orders: \", region.granules.orderIDs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'overcast' == project_name:\n",
    "    path = '../jovyan/shared/data-overcast/'+ roi.upper() + '/' + short_name\n",
    "else:\n",
    "    path = './download/' + roi.upper() + '/' + short_name\n",
    "\n",
    "region.download_granules(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
